<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
require_once("content_negotiation/include.php");

/**
 * Resource
 * 
 * Redirect a user to the external URL or attachment linked to an aggregated resource, or to the HTML or RDF representation if no external URL or attachment is available.
 */
class escape_repositoryui_action_Resource extends escape_repositoryui_Action
{
	public function handleRequest()
	{
		/* @var $repository escape_repository_Repository */
		$repository =& $this->requestAttributes['repository'];
				
		// fetch the ID from the request
		$aggregatedResourceId = $_REQUEST['actionValue'];
		$aggregatedResourceUri = $repository->convertDataObjectIdToObjectUri($aggregatedResourceId);
				
		// get the aggregated resource
		try {
			$aggregatedResource = $repository->getOreAggregatedResourceByUri($aggregatedResourceUri);
		}
		catch(Exception $exception)
		{
			// couldn't find one of the resources
			header("HTTP/1.1 404 Not Found");
			header("Status: 404 Not Found");
			header('Content-Type: text/plain');
			print "Aggregated resource not found!\n";
			
			return null;
		}
		
		// perform content negotiation, unless the browser is MSIE (which has a broken accept header)
		$sendRdf = false;
		if(!escape_isBrowserInternetExplorer())
		{
			// prefer RDF/XML unless the client prefers (X)HTML
			$contentMatch = array(
								'type' => array(
												0 => 'application/xhtml+xml',
												1 => 'text/html',
												2 => 'application/rdf+xml',
											),
								'qFactorApp' => array(
												0 => 0.9,
												1 => 0.9,
												2 => 1,
											),
								);
			
			$bestMatch = conNeg::mimeBest($contentMatch, false);
			if($bestMatch == 'application/rdf+xml')
			{
				$sendRdf = true;
			}
		}
		
		// fetch the aggregation URL
		$aggregation = $aggregatedResource->getAggregation();
		$aggregationUrl = $aggregation->getExternalUri();
		
		// fetch the resource map URL
		$remUrl = $aggregation->getRemExternalUri();
		
		// add link to aggregation to headers
		header('Link: <' . $aggregationUrl . '>; rel="aggregation"', true);
		// add link to resource map to headers
		header('Link: <' . $remUrl . '>; type="application/rdf+xml"; rel="resourcemap"', false);
	
		if($sendRdf)
		{
			header('Location: ' . $repository->config['server_url'] . '/rdf/' . $aggregatedResourceId, true, 303);
		}
		else
		{
			if($aggregatedResource->getAttachmentMimeType() !== null)
			{
				// binary attachment available, redirect to attachment 
				header('Location: ' . $repository->config['server_url'] . '/attachment/' . $aggregatedResourceId, true, 303);
			}
			else
			{
				// no attachment available
				
				// check if an external URI is available and if it points to a location outside of the current server
				$externalUri = $aggregatedResource->getExternalUri();
				$resourceUri = $aggregatedResource->getResourceUri();
				if($externalUri === $resourceUri)
				{
					// redirect to the show page
					header('Location: ' . $repository->config['server_url'] . '/show/' . $aggregatedResourceId, true, 303);
				}
				else
				{
					// redirect to external URI
					header('Location: ' . $externalUri, true, 303);
				}
			}
		}
		
		return null;
	}
}